<template>
  <div class='container'>
    <el-card>
      <el-row>
        <el-col :span="18">
          <el-form  :inline="true" :model="params" ref="params">
            <el-form-item label="学科名称" size="small" :prop="params.subjectName">
              <el-input placeholder="请输入学科名称" v-model.trim="params.subjectName" class="query-ipt" size="small"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button  size="small" @click="hReset">清除</el-button>
              <el-button type="primary" @click="hSerch" size="small">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      <el-button
      class="btn"
      size="small"
      type="success"
      icon="el-icon-edit"
      @click="hSubADD"
      >新增学科</el-button>
      </el-row>
      <el-alert
        v-if="cResultTip"
        :title="cResultTip"
        :closable="false"
        type="info"
        style="margin-bottom:20px"
        show-icon>
      </el-alert>
      <el-table
        highlight-current-row
        :data="subObj.items"
        v-loading="isLoading"
        style="width: 100%">
        <el-table-column
          prop="id"
          label="序号"
          width="50"
          type="index"
          align="center"
          >
        </el-table-column>
        <el-table-column
          prop="subjectName"
          label="学科名称"
          align="center"
          >
        </el-table-column>
        <el-table-column
          prop="username"
          align="center"
          label="创建者">
        </el-table-column>
        <el-table-column
          prop="addDate"
          align="center"
          :formatter="formatTime"
          width="200"
          label="创建日期">
        </el-table-column>
        <el-table-column
          align="center"
          label="前台是否显示">
          <template slot-scope="scope">
            {{scope.row.isFrontDisplay?'是':"否"}}
          </template>
        </el-table-column>
        <el-table-column
          prop="twoLevelDirectory"
          align="center"
          label="二级目录">
        </el-table-column>
        <el-table-column
          prop="tags"
          align="center"
          label="标签">
        </el-table-column>
        <el-table-column
          prop="totals"
          align="center"
          label="题目数量">
        </el-table-column>
        <el-table-column
          prop="address"
          align="center"
          label="操作"
          width="250"
          >
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="hClassify(scope.row)">学科分类</el-button>
            <el-button
              type="text"
              @click="hTags(scope.row)">学科标签</el-button>
            <el-button
              type="text"
              name="edit"
              @click="hEdit(scope.row.id)">修改</el-button>
            <el-button
              type="text"
              @click="hDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <page-Tool
        :paginationPagesize="params.pagesize"
        :paginationPage ="params.page"
        :total="subObj.counts"
        @pageChange="hCurrentChange"
        @pageSizeChange="hSizeChange"
        >
      </page-Tool>
    </el-card>
    <!-- 对话框 -->
    <subjects-Add
    v-model="flag"
    :subId="subId"
    v-if="flag"
    @init="hFixPage"
    ></subjects-Add>
  </div>
</template>

<script>
import { list, remove } from '@/api/hmmm/subjects.js'
import pageTool from '@/module-manage/components/page-tool.vue'
import subjectsAdd from '@/module-hmmm/components/subjects-add.vue'
export default {
  name: 'subjects',
  components: {
    subjectsAdd,
    pageTool
  },
  data () {
    return {
      // dialog开关
      flag: false,
      // 信息总数
      total: '',
      // 学科渲染数据
      subObj: {},
      params: {
        // 当前页数
        page: 1,
        // 每页显示多少
        pagesize: 10,
        subjectName: ''
      },
      subId: null,
      // 设置 节流定时器
      timer: null,
      // 加载效果
      isLoading: false
    }
  },
  computed: {
    cResultTip () {
      return `共 ${this.total} 条记录`
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 解决page 和新增不回到第一页bug
    hFixPage () {
      if (!this.subId) {
        this.params.page = 1
        this.getList()
      } else {
        this.getList()
      }
    },
    // 获取学课列表
    async getList () {
      try {
        this.isLoading = true
        const res = await list(this.params)
        this.total = res.data.counts
        this.subObj = res.data
        this.isLoading = false
      } catch (error) {
        console.log(error)
      }
    },
    // 搜索按钮
    hSerch () {
      // 设置节流阀,控制连续点击发送请求
      if (this.timer) return
      this.timer = setTimeout(() => {
        this.params.page = 1
        this.getList()
        this.timer = null
      }, 500)
    },
    // 重置按钮
    hReset () {
      if (this.params.subjectName) this.params.subjectName = ''
    },
    // 学科新增按钮
    hSubADD () {
      this.subId = null
      this.flag = true
    },
    // 时间
    formatTime (row) {
      const date = new Date(row.addDate)
      var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
      var month = date.getMonth() + 1
      month = month < 10 ? '0' + month : month
      var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
      var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
      var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
      return date.getFullYear() + '/' + month + '/' + day + ' ' + hours + ':' + minutes + ':' + seconds
    },
    // 学科分类按钮
    hClassify (row) {
      this.$router.push({
        path: 'directorys',
        query: {
          id: row.id,
          name: row.subjectName
        }
      })
    },
    // 学科标签按钮
    hTags (row) {
      this.$router.push({
        path: 'tags',
        query: {
          id: row.id,
          name: row.subjectName
        }
      })
    },
    // 学科修改按钮
    hEdit (subId) {
      this.subId = subId
      this.flag = true
    },
    // 学科删除
    async hDelRequest (params) {
      try {
        await remove(params)
        this.getList()
      } catch (error) {
        console.log(error)
      }
    },
    hDel (val) {
      this.$confirm('确认要删除嘛?', '确认信息', {
        distinguishCancelAndClose: true,
        confirmButtonText: '确认',
        cancelButtonText: '放弃删除'
      })
        .then(() => {
          this.hDelRequest(val)
          this.$message.success('删除成功')
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 分页
    hCurrentChange (val) {
      this.params.subjectName = null
      this.params.page = val
      this.getList()
    },
    hSizeChange (val) {
      this.params.page = 1
      this.params.pagesize = val
      this.getList()
    }
  }
}
</script>

<style scoped lang='scss'>

.query-ipt {
  width: 185px
}
.btn {
  float: right;
}
</style>
